package com.lg.wxq.manager;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.lg.wxq.bean.Status;
import com.lg.wxq.bean.Task;
import com.lg.wxq.config.Strings;
import com.lg.wxq.config.TaskType;
import com.lg.wxq.config.Urls;
import com.lg.wxq.utils.BeanUtils;
import com.lg.wxq.utils.DebugUtils;
import com.lg.wxq.utils.StringUtils;
import com.weibo.net.Weibo;
import com.weibo.net.WeiboException;

public class WeiboManager {
	private Weibo weibo;
	private ActivityManager activityManager;
	private ImageManager imageManager;
	private TaskManager taskManager;

	private static WeiboManager instance = new WeiboManager();

	private WeiboManager() {
		weibo = Weibo.getInstance();
		activityManager = ActivityManager.getInstance();
		imageManager = ImageManager.getInstance();
		taskManager = TaskManager.getInstance();
	}

	public static WeiboManager getInstance() {
		return instance;
	}

	public Task request(Task task) {
		try {
			String request = weibo.request(activityManager.getActivity(task.getActID()), task.getUrl(), task.getParameters(), task.getRequestMethod(), weibo.getAccessToken());
			return getCallBackTask(task,request);
		} catch (WeiboException e) {
			DebugUtils.exception(e);
			return task;
		}

	}

	private Task getCallBackTask(Task task, String request) {
		switch (task.getTaskType()) {
		case TaskType.TS_STATUSES_HOME_TIMELINE:
			DebugUtils.debug("request:"+request);
			ArrayList<Status> statuses = getHomeTimeline(request);
			newImageTask(statuses,task.getActID());
			task.setData(statuses);
			break;
		case TaskType.TS_STATUSES_UPDATE:
			Status status=getNewWeibo(request);
			newImageTask(status,task.getActID());
			DebugUtils.debug("发微博："+request);
			task.setData(status);
			break;
		}
		return task;
	}

	private Status getNewWeibo(String request) {
		try {
			JSONObject jo = new JSONObject(request);
			return BeanUtils.jsonObjectToBean(Status.class, jo);
		} catch (JSONException e) {
			DebugUtils.exception(e);
			return null;
		}
		
	}

	private ArrayList<Status> getHomeTimeline(String request) {
		try {
			JSONObject jo = new JSONObject(request);
			JSONArray jsonArray = jo.getJSONArray("statuses");
			ArrayList<Status> statusList = new ArrayList<Status>();
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				Status status = BeanUtils.jsonObjectToBean(Status.class, jsonObject);
				statusList.add(status);
			}
			return statusList;
		} catch (JSONException e) {
			DebugUtils.exception(e);
			return null;
		}
	}

	private Task getTask(int actID, int taskType, String url, String id) {
		Task task = new Task(actID, taskType);
		task.setUrl(url);
		task.setData(id);
		return task;
	}
	
	private void newImageTask(ArrayList<Status> statuses, int actID) {
		for (int i = 0; i < statuses.size(); i++) {
			newImageTask(statuses.get(i),actID);
			
		}
	}
	
	

	private void newImageTask(Status status,int actID) {
		if (imageManager.getAvatar(status.getUser().getIdstr()) == null) {
			taskManager.addImage(getTask(actID, TaskType.TS_GETIMAGE_AVATAR, status.getUser().getProfile_image_url(), status.getUser().getIdstr()));
		}
		if (StringUtils.isNotBlank(status.getThumbnail_pic())) {
			if (imageManager.getPicture(status.getIdstr()) == null) {
				taskManager.addImage(getTask(actID, TaskType.TS_GETIMAGE_PICTURE, status.getThumbnail_pic(), status.getIdstr()));
			}
		}
		if (status.getRetweeted_status() != null) {
			if (StringUtils.isNotBlank(status.getRetweeted_status().getThumbnail_pic())) {
				if (imageManager.getPicture(status.getRetweeted_status().getIdstr()) == null) {
					taskManager.addImage(getTask(actID, TaskType.TS_GETIMAGE_PICTURE, status.getRetweeted_status().getThumbnail_pic(), status.getRetweeted_status()
							.getIdstr()));
				}
			}
		}
	}
	
	
}
